<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\User;
use app\common\model\shopmanager\Shop as ShopModel;
use app\common\model\printmanager\Order;
use app\common\model\printmanager\Goods;
use think\Db;
use think\Config;
use fast\Random;

/**
 * 我的小店接口
 */
class Shop extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     *
     */
    public function index()
    {
        $this->success('', ['welcome' => '123']);
    }

    /*
     * 我的小店申请
     */
    public function shopApply(){
        $user_id = $this->auth->getUserinfo()['id'];
        $item_num = User::where(array('top_id'=>$user_id))->count();    //推荐人数量
        $hunter_limit = config('site.hunter_limit');    //限制人数
        
        if($item_num < $hunter_limit){
            $this->error('需邀请'.$hunter_limit.',您还差'.($hunter_limit-$item_num).'人');
        }
        $where['user_id'] = $user_id;
        $allow_apply = ShopModel::where($where)->field('`status`')->find();
        if($allow_apply){
            if($allow_apply['status'] == 0){
                $this->error('申请等待审核，请勿重复提交');
            }

            if($allow_apply['status'] == 1){
                $this->error('申请已审核通过，请勿重复提交');
            }
        }

        $data = $this->request->post();
        $data['user_id'] = $user_id;
        unset($data['token']);
        if(isset($data['share_from'])) unset($data['share_from']);

        $is_success = ShopModel::create($data);
        $this->success('提交成功', $is_success);
    }

    /*
     * 我的小店信息
     */
    public function getShopInfo(){
        $user_id = $this->auth->getUserinfo()['id'];
        $where['user_id'] = $user_id;
        $where['status'] = array('in', '0, 1');
        $field = 'id, real_name, gender, province_id, city_id, county_id, age, profession, mobile,
            createtime, user_id';
        $huter_info = ShopModel::where($where)->field($field)->find();
        $this->success('我的小店信息', $huter_info);
    }

    /*
     * 我的客户
     */
    public function shopMember(){
        $user_id = $this->auth->getUserinfo()['id'];
        $where['user_id'] = $user_id;
        $where['status'] = 1;
        $field = 'id, createtime';
        $shop_info = ShopModel::where($where)->field($field)->find();
        if(!$shop_info){
            $this->error('您的小店尚未开启');
        }
        //我推荐的人 下的订单
        //$invitation = Db::name('self_invitation')->where(array('user_id'=>$user_id))->value('to_user_id'); 
        $invitation = User::where(array('top_id'=>$user_id))->column('id');

        $where['user_id'] = array('in', $invitation);
        $where['status'] = 3;
        $field = 'id, user_id, amount, createtime';
        $order_list = Order::where($where)->field($field)->order('createtime')->paginate()->toArray();
        $shop_rate = config('site.shop_rate');
        $shop_rate = $shop_rate ? $shop_rate : 0;
        foreach($order_list['data'] as $key => $val){
            //用户昵称
            $order_list['data'][$key]['nickname'] = User::where(array('id'=>$val['user_id']))->value('nickname');
            //商品名称
            $goods_id = Db::name('self_order_goods')->where(array('order_id'=>$val['id']))->value('goods_id');
            $order_list['data'][$key]['goods_name'] = Goods::where(array('id'=>$goods_id))->value('name');
            $order_list['data'][$key]['createtime_group'] = date('Y年m月', $val['createtime']);
            $order_list['data'][$key]['createtime_buy'] = date('m月d日 H:i', $val['createtime']);
            $order_list['data'][$key]['profit'] = $shop_rate == 0 ? 0 : round($val['amount'] * $shop_rate / 100, 2);
        }

        $this->success('我的客户', $order_list);

    }

    
}
